 <template>
   <view style="width: 100%;height: 100%;">
     <view class="root">
       <view class="root-inner" :style="{display: !visibleCamera ? 'flex': 'none'}">
         <view class="content">
           <uni-forms ref="formRef" :model="form" validateTrigger="blur" label-position="top" label-width="100%"
             err-show-type="toast">
             <view class="content-wrapper">
               <yy-title title="电瓶信息"></yy-title>
               <view class="content-inner-wrapper">
                 <uni-forms-item label="电瓶品牌：" name="brand" key="brand">
                   <view class="form-content-item" :class="form.brand ?  '' : 'input-item'">
                     <uni-easyinput type="text" :maxlength="30" v-model="form.brand" :inputBorder="false"
                       placeholder="请填写" :clearable="true" />
                   </view>
                 </uni-forms-item>
                 <uni-forms-item label="电压伏数(V)：" name="voltage" key="voltage">
                   <view class="form-content-item" :class="form.voltage ?  '' : 'input-item'">
                     <uni-easyinput type="number" :maxlength="10" v-model="form.voltage" :inputBorder="false"
                       placeholder="请填写" :clearable="true" />
                   </view>
                 </uni-forms-item>
                 <uni-forms-item label="电瓶数量：" name="batterySum" required key="batterySum">
                   <view class="form-content-item" :class="form.batterySum ?  '' : 'input-item'">
                     <uni-easyinput type="number" :maxlength="10" v-model="form.batterySum" :inputBorder="false"
                       placeholder="请填写" :clearable="true" />
                   </view>
                 </uni-forms-item>
                 <uni-forms-item label="电瓶图片:（最多可上传3张图片，必填）" required name="saPathList" style="margin-bottom: 0;">
                   <u-upload :fileList="form.saPathList" @afterRead="(info) => afterReadFive(info, 'form', 80)"
                     @delete="(info) => deletePhoto(info, 'form')" name="saPathList" :maxCount="3" :multiple="true"
                     width="96" height="80">
                     <image :src="baseImgUrl + 'upload-icon3.png'" style="width: 200rpx; height: 160rpx"></image>
                   </u-upload>
                 </uni-forms-item>
                 <uni-forms-item label="回收时间:" name="recycleTime" required key="recycleTime">
                   <view class="form-content-item" :class="form.recycleTime ?  '' : 'input-item'">
                     <uni-datetime-picker v-model="form.recycleTime" type="datetime" :hide-second="true"
                       :end="getEndOfTodayTimestamp()">
                       <uni-easyinput type="text" v-model="form.recycleTime" :inputBorder="false" placeholder="请选择"
                         :clearable="true" disabled />
                     </uni-datetime-picker>
                     <image :src="baseImgUrl + 'day-icon2.png'" style="width: 30rpx;height: 30rpx;padding:0 10rpx;">
                   </view>
                 </uni-forms-item>
               </view>
             </view>
             <view class="content-wrapper">
               <yy-title title="售卖人信息"></yy-title>
               <uni-forms-item label="身份证号:" name="sellPersonIdcard" required key="sellPersonIdcard">
                 <view class="form-content-item" :class="form.sellPersonIdcard ?  '' : 'input-item'">
                   <uni-easyinput type="idcard" :maxlength="18" v-model="form.sellPersonIdcard" :inputBorder="false"
                     placeholder="请填写" :clearable="true" />
                   <u-icon slot="right" size="21" name="scan" color="#2488F5" @click="handleCamera"></u-icon>
                 </view>
               </uni-forms-item>
               <uni-forms-item label="姓名:" name="sellPersonName" required key="sellPersonName">
                 <view class="form-content-item" :class="form.sellPersonName ?  '' : 'input-item'">
                   <uni-easyinput type="text" :maxlength="30" v-model="form.sellPersonName" :inputBorder="false"
                     placeholder="请填写" :clearable="true" />
                 </view>
               </uni-forms-item>
               <uni-forms-item label="联系电话:" name="sellPersonMobile" key="sellPersonMobile">
                 <view class="form-content-item" :class="form.sellPersonMobile ?  '' : 'input-item'">
                   <uni-easyinput type="number" :maxlength="11" v-model="form.sellPersonMobile" :inputBorder="false"
                     placeholder="请填写" :clearable="true" />
                 </view>
               </uni-forms-item>
               <uni-forms-item label="人脸信息:（最多可上传3张图片，必填）" required name="personPathList" style="margin-bottom: 0;">
                 <u-upload :fileList="form.personPathList" @afterRead="(info) => afterReadFive(info, 'form', 81)"
                   @delete="(info) => deletePhoto(info, 'form')" name="personPathList" :maxCount="3" :multiple="true"
                   width="96" height="80">
                   <image :src="baseImgUrl + 'upload-icon3.png'" style="width: 200rpx; height: 160rpx"></image>
                 </u-upload>
               </uni-forms-item>
             </view>
           </uni-forms>
         </view>
         <view class="bottom">
           <yy-button-submit @submit="onSubmit"></yy-button-submit>
         </view>
       </view>
       <yy-photo-mask :visible.sync="visibleCamera" :baseImgUrl="baseImgUrl" :clientId="clientId"
         :clientSecret="clientSecret" @parsedSuccess="onParsedSuccess"></yy-photo-mask>
     </view>
   </view>
 </template>

 <script>
   import {
     storagebatteryAcquisitionRecordsSave
   } from '../api/api'
   import uploadFileMixin from '../utils/upload-file-mixin'
   import {
     Toast,
     getEndOfTodayTimestamp,
     getOptionsParams,
     idCardRule,
     isLoading,
     navBack,
     phoneRule
   } from '@/utils/util.js'
   export default {
     mixins: [uploadFileMixin],
     data() {
       return {
         baseImgUrl: this.$baseImgUrl,
         clientId: this.$clientId,
         clientSecret: this.$clientSecret,
         visibleCamera: false,
         form: this.initForm(),
         routerParams: {},
         rules: {
           brand: {
             rules: [{
               required: false,
               errorMessage: "请填写电瓶品牌！",
             }]
           },
           batterySum: {
             rules: [{
               required: true,
               errorMessage: "请填写电瓶数量！",
             }]
           },
           voltage: {
             rules: [{
               required: false,
               errorMessage: "请填写电压伏数！",
             }]
           },
           saPathList: {
             rules: [{
               required: true,
               errorMessage: "请上传电瓶图片！",
             }]
           },
           recycleTime: {
             rules: [{
               required: true,
               errorMessage: "请选择回收时间！",
             }]
           },
           sellPersonIdcard: {
             rules: idCardRule()
           },
           sellPersonName: {
             rules: [{
               required: true,
               errorMessage: "请填写姓名！",
             }]
           },
           sellPersonMobile: {
             rules: phoneRule(false)
           },
           personPathList: {
             rules: [{
               required: true,
               errorMessage: "请上传人脸照片！",
             }]
           }
         }
       }
     },
     onLoad(options) {
       this.routerParams = getOptionsParams(options)
     },
     onReady() {
       this.$refs.formRef.setRules(this.rules)
     },
     methods: {
       getEndOfTodayTimestamp,
       initForm() {
         return {
           brand: '',
           voltage: '',
           batterySum: '',
           recycleTime: '',
           sellPersonIdcard: '',
           sellPersonName: '',
           sellPersonMobile: '',
           saPathList: [],
           personPathList: []
         }
       },
       onSubmit() {
         this.$refs.formRef.validate().then(() => {
             let data = {
               companyMessageId: this.routerParams.companyMessageId,
               ...this.form,
             };

             // 处理图片信息
             if (data.saPathList) {
               data.saPathList = data.saPathList.map(item => {
                 return item.halfPath
               })
             }
             if (data.personPathList) {
               data.personPathList = data.personPathList.map(item => {
                 return item.halfPath
               })
             }
             isLoading(true)
             storagebatteryAcquisitionRecordsSave(data).then((res) => {
                 if (res.code == 200) {
                   setTimeout(() => {
                     navBack();
                   }, 100)
                   uni.$emit('add-battery-success')
                 }
                 Toast(res.message);
               })
               .finally(() => {
                 isLoading(false)
               })
           })
           .catch(() => {})
       },
       handleCamera() {
         this.visibleCamera = true;
       },
       onParsedSuccess(value) {
         const {
           domicileAddress,
           userName,
           idcard,
           nation
         } = value;
         this.$set(this.form, "sellPersonName", userName);
         this.$set(this.form, "sellPersonIdcard", idcard);
         this.visibleCamera = false;
       },
     }
   }
 </script>

 <style lang="scss" scoped>
   @import '@/styles/uni-form-label-top.scss';

   .root {
     display: flex;
     width: 100%;
     height: 100%;
     flex-direction: column;
     background: #F9F9F9;

     .root-inner {
       display: flex;
       width: 100%;
       height: 100%;
       flex-direction: column;
     }

     .content {
       flex: 1;
       height: 0;
       overflow: auto;

       .content-wrapper {
         background: #FFFFFF;
         box-shadow: 0rpx 3rpx 6rpx 1rpx rgba(207, 207, 207, 0.1608);
         border-radius: 8rpx 8rpx 8rpx 8rpx;
         padding: 40rpx 20rpx;
         margin-bottom: 10rpx;
       }

       .pawn-list-wrapper {
         background: #F9F9F9;
         padding: 25rpx;
         margin-bottom: 10rpx;

         .title-name {
           display: flex;
           justify-content: space-between;
           margin-bottom: 23rpx;

           .name-i {
             font-weight: bold;
             font-size: 32rpx;
             color: #181818;
           }

           .delete-i {
             display: flex;
             align-items: center;
             font-weight: 500;
             font-size: 26rpx;
             color: #F94A4A;
             flex-shrink: 0;

             .text-delete {
               margin-left: 10rpx;
             }
           }
         }

         .i-item {
           margin-bottom: 15rpx;
           display: flex;

           .i-label {
             font-weight: 500;
             font-size: 28rpx;
             color: #999999;
             width: 119rpx;
             margin-right: 16rpx;
             position: relative;
             text-align: justify;
             text-align-last: justify;
             flex-shrink: 0;



             &::after {
               content: ':';
               position: absolute;
               right: -8rpx;
             }
           }

           .i-value {
             font-weight: bold;
             font-size: 28rpx;
             color: #101010;
           }

         }
       }

       .datetime-picker-wrapper {
         display: flex;
         align-items: center;
         justify-content: space-between;

         .text-t {
           font-weight: 500;
           font-size: 30rpx;
           color: #666666;
         }

         .t-item {
           font-weight: 400;
           font-size: 28rpx;
           color: #A2A2A2;
           width: 304rpx;
           height: 70rpx;
           background: #F9F9F9;
           border-radius: 8rpx 8rpx 8rpx 8rpx;
           border: 1rpx solid #F1EEEE;
           display: flex;
           align-items: center;
           justify-content: center;
         }

         .has-value {
           font-weight: bold;
           font-size: 30rpx;
           color: #181818;
         }
       }
     }
   }
 </style>